package com.labor_management_system.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.labor_management_system.Entity.Enterprises;
import com.labor_management_system.Entity.LaborEngineering;
import com.labor_management_system.Entity.WorkDetails;
import com.labor_management_system.Mapper.LaborEngineeringMapper;
import com.labor_management_system.Mapper.WorkDetailsMapper;
import com.labor_management_system.Service.EnterprisesService;
import com.labor_management_system.Mapper.EnterprisesMapper;
import com.labor_management_system.Utils.FileTypeUtil;
import com.labor_management_system.Utils.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
* @author deathwing475
* @description 针对表【enterprises】的数据库操作Service实现
* @createDate 2024-02-27 09:54:41
*/
@Service
public class EnterprisesServiceImpl extends ServiceImpl<EnterprisesMapper, Enterprises>
    implements EnterprisesService {


    @Override
    public List<Pair<String, String>> getEnterprisesIDAndName() {
        List<Enterprises> t = baseMapper.selectList(null);
        List<Pair<String, String>> backlist= new ArrayList<>();
        for(Enterprises p : t){
            Pair<String, String> s = new Pair<>(p.getEnterprisesId(),p.getEnterprisesName());
            backlist.add(s);
        }
        return backlist;
    }

    //新建单位
    @Override
    public Boolean upload(Enterprises enterprises, MultipartFile multipartFile) throws IOException {

        if(multipartFile!=null){
            enterprises.setEnterprisesId(UUID.randomUUID().toString().replace("-",""));

            String filepath = "C://Users//16683//Desktop//ppgg//"+enterprises.getEnterprisesName()+"."+ FileTypeUtil.getType(multipartFile).name();
            enterprises.setProofOfBusiness(filepath);

            //存入磁盘
            File saveFile = new File(filepath);
            multipartFile.transferTo(saveFile);

            //保存数据库
            int insert = baseMapper.insert(enterprises);

            return  true;

        }

        return  false;
    }

    //删除单位
//    @Override
//    public Boolean deleteUnitById(String enterprisesId) {
//        //查询主表的数据
//        Enterprises enterprises = enterprisesMapper.selectById(enterprisesId);
//
//        //如果主表数据存在
//        if(enterprises !=null){
//            //查询关联表的外键数据enterprises_id
//            List<LaborEngineering> laborEngineeringList = laborEngineeringMapper.selectList(new QueryWrapper<LaborEngineering>().eq("enterprises_id", enterprisesId));
//
//            //逐一处理关联表的数据
//            for(LaborEngineering laborEngineering:laborEngineeringList){
//                List<WorkDetails> workDetailsList = workDetailsMapper.selectList(new QueryWrapper<WorkDetails>().eq("labor_engineering_id", laborEngineering.getLaborEngineeringId()));
//
//                //逐一删除第三张表的的外键数据
//                for(WorkDetails workDetails:workDetailsList){
//                    workDetailsMapper.deleteById(workDetails.getWorkDetailsId());
//                }
//
//                // 最后删除关联表的数据
//                laborEngineeringMapper.deleteById(laborEngineering.getLaborEngineeringId());
//            }
//
//            // 最后删除主表的数据
//            enterprisesMapper.deleteById(enterprisesId);
//
//            return true;
//        }
//
//        return false;
//    }

    //显示所有单位
    @Override
    public List<Enterprises> showAllUnits(Integer selectPage, Integer PageSize) {
        Page<Enterprises> Page = new Page<>(selectPage, PageSize);
        baseMapper.selectPage(Page,null);

        return Page.getRecords();
    }

    @Override
    public List<Enterprises> findByName(String enterprisesName, Integer enterprisesCategory) {
        QueryWrapper<Enterprises> queryWrapper =new QueryWrapper<>();

        queryWrapper.eq("enterprises_name",enterprisesName);

        if (enterprisesCategory != null) {
            queryWrapper.eq("enterprises_category", enterprisesCategory);
        }

        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public boolean updateUnits(Enterprises enterprises, MultipartFile multipartFile) throws IOException {
        if(multipartFile!=null){

            String filepath = "C://Users//16683//Desktop//ppgg//"+enterprises.getEnterprisesName()+"."+ FileTypeUtil.getType(multipartFile).name();
            enterprises.setProofOfBusiness(filepath);

            //存入磁盘
            File saveFile = new File(filepath);
            multipartFile.transferTo(saveFile);

            //更新数据库
            QueryWrapper<Enterprises> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("enterprises_id",enterprises.getEnterprisesId());

            baseMapper.update(enterprises,queryWrapper);


            return  true;

        }

        else if (multipartFile==null){
            //更新数据库
            QueryWrapper<Enterprises> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("enterprises_id",enterprises.getEnterprisesId());

            baseMapper.update(enterprises,queryWrapper);


            return  true;
        }

        return  false;
    }
    //企业单位
    @Override
    public List<Enterprises> selectAllUnit() {
        return baseMapper.selectList(null);
    }
}




